package com.danskin.wechat.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.danskin.wechat.dao.CustCouponRecordDao;
import com.danskin.wechat.dao.CustInfoExDao;
import com.danskin.wechat.vo.CustCouponApiView;
import com.danskin.wechat.entity.CustCouponRecord;
import com.danskin.wechat.entity.CustInfoEx;
import com.danskin.wechat.entity.CustInfoExView;
import com.danskin.wechat.entity.WxMediaPlatform;
import com.danskin.wechat.service.CouponService;
import com.danskin.wechat.service.WechatMsgTemplateService;
import com.danskin.wechat.util.LogUtil;
import com.danskin.wechat.util.Page;

@Service
public class CouponServiceImpl extends BaseServiceImpl implements CouponService {

	@Autowired
	public CustCouponRecordDao couponDao;
	@Autowired
	private CustInfoExDao custInfoExDao;

	@Autowired
	private WechatMsgTemplateService msgTemplateService;

	@Override
	public int saveCustCouponRecord(CustCouponRecord entity, WxMediaPlatform mp) {
		CustInfoExView custInfoEx = custInfoExDao.selectCustByMemberId(mp.getId(), entity.getMemberId());
		if (custInfoEx != null) {

			int insertCount = 0;

			entity.setCustExId(custInfoEx.getId());
			try {
				CustCouponRecord couponRecord = couponDao.selectByCustExIdAndCouponNo(custInfoEx.getId(),
						entity.getCouponNo());
				if (couponRecord == null) {
					insertCount = couponDao.insertSelective(entity);
					//add("新增用户领取优惠券记录", "优惠券数据接收模块");

					if (entity.getIsUsed().equals(0)) {
						msgTemplateService.sendCustGetCouponMsg(mp.getAccessToken(), mp.getAppId(),
								custInfoEx.getWxOpenId(), custInfoEx.getName(), entity.getName());
					}
				} else {
					entity.setId(couponRecord.getId());
					entity.setLastModifiedTime(new Date());
					
					couponDao.updateByPrimaryKeySelective(entity);
					//add("修改用户领取优惠券记录", "优惠券数据接收模块");
					
					// 如果券标记为已使用，发送微信消息模版通知
					if (couponRecord.getIsUsed().equals(0) && entity.getIsUsed().equals(1)) {
						msgTemplateService.sendCustUseCouponMsg(mp.getAccessToken(), mp.getAppId(),
								custInfoEx.getWxOpenId(), custInfoEx.getName(), entity.getName());
					}
				}
			} catch (Exception e) {
				LogUtil.error("========新增用户领取优惠券记录异常", e);
			}
			return insertCount;
		}
		return -1;
	}

	@Override
	public List<CustCouponApiView> listPageByOpenId(Page page, String type) {
		Map<String, Object> map = page.getMap();
		if (type.equals("usable")) {
			map.put("isUsed", 0);
		} else if (type.equals("used")) {
			map.put("isUsed", 1);

		} else if (type.equals("overtime")) {
			map.put("expiredTime", new Date());
		}
		List<CustCouponApiView> list = couponDao.listPageByOpenId(page);

		return list;
	}

	@Override
	public CustCouponApiView findCustCouponView(int couponId, String appId) {

		return couponDao.selectViewById(couponId, appId);
	}

	@Override
	public int setCustCouponWxGet(int mpId, String couponNo) {
		CustCouponRecord entity = new CustCouponRecord();
		entity.setCouponNo(couponNo);
		return couponDao.updateIsWxGetByCouponNo(entity);
	}

	@Override
	public CustCouponRecord findCustCoupon(int couponId, String couponNo) {

		return couponDao.selectByIdAndCouponNo(couponId, couponNo);
	}

	@Override
	public int selectAvaCouponByCondition(CustCouponRecord custCouponRecord) {
		int i = couponDao.selectAvaCouponByCondition(custCouponRecord);
		return i;
	}

}
